/**
 * Created by kevin on 2017/01/08.
 *
 * page base
 */


var eosi = window.eosi || {};
this.eosi.website = this.eosi.website || {};

(function (window) {
    // the application


    function BasePage_globalload(config) {

        this.Container_constructor();
        this.config = config;
        //console.log(this.config)
        this.init();
        //console.log(this.father.targetWidth);

    }


    var p = createjs.extend(BasePage_globalload, eosi.website.BasePage);
    p.className = "BasePage_globalload";

    // public methods:
    p.init = function () {
        // this.loadRes();//K
        var that=this;
        setTimeout(function(){
                that.loadComplete()//L
        },200)
        this.on("removed",this.distoryPage,this);

    }

    p.addloaded_img=function(){
        this.resItems=[];
        for (var i = 0; i <this.loader.resItems.length; i++) {
                for (var j in this.loader.resItems[i]["Pages"]) {
                    if (j==this.name) {
                        this.resItems.push(this.loader.resItems[i]);
                        break;
                    }
                }
            // }
        }
        // for (var i = 0; i <this.resItems.length; i++) {
        //     console.log(this.resItems[i].id);
        // }
    }
    /*
     * 资源加载完毕
     * */
    p.loadComplete = function (evt){
        this.addloaded_img();
        // this.loader.resItems["Pages"][0]


        if(LAND_PAGE==null){
            LAND_PAGE = this;
        }
        this.resIsLoaded = true;
        this.config.resIsLoaded = this.resIsLoaded;
        //console.log(cur_page,this.name)
        if(cur_page == this.name){
            this.drawPage();
        }

        // onComplete(evt);
        contentLayer.preload(this.pageIndex);

    }
    p.onProgress = function(evt){
        // console.log(evt);
        
        // if(LAND_PAGE){
        //     onProgress(evt);
        // }else{
            // onProgress(evt,CORE_RES_SIZE_PER,1-CORE_RES_SIZE_PER);
        // }

    }
    /*
    *绘制页面
    * */
    p.drawPage = function(){
        for(var i = 0 ;i<this.resItems.length;i++){
            if(this.resItems[i].type == "manifest"){
                return;
            }
            // console.log(this.resItems[i]["Pages"][this.name]);
            // console.log(this.resItems[i].id);
            // console.log(this.loader.loader.getResult(this.resItems[i].id))
            var pic = this.loader.loader.getResult(this.resItems[i].id);
            var item;
            if (this.resItems[i]["Pages"][this.name]) {
            }else{
                continue;
            }
            switch(this.resItems[i]["Pages"][this.name].r_type.type){
                case "bitmap":
                    item = new createjs.Bitmap(pic);
                    break;
                case "switch":
                    var bitmap = new createjs.Bitmap(pic);
                    item = new eosi.website.BaseSwitch(bitmap,i,this);
                    break
                case "spritesheet":
                    var draw_content=this.resItems[i]["Pages"][this.name].prop["draw"];//精灵绘制配置对象
                    item = new eosi.website.BaseSpriteSheet(pic,draw_content);
                    break;
                case "baseslide":
                    item = new eosi.website.BaseSlide(pic);
                    break;
                case "button":
                    var bitmap = new createjs.Bitmap(pic);
                    item = new eosi.website.BaseButton(bitmap);
                    //item.addChild(bitmap);
                    break;
                case "class":
                    var temClass = eval(this.resItems[i]["Pages"][this.name].r_type.className);
                    item = new temClass(this.resItems[i]["Pages"][this.name],this.resItems[i]["src"]);
                    //console.log(this.loader.getResult(this.resItems[i].id))
                    //item = new temClass(this.loader.getResult(this.resItems[i].id));
                    //console.log(this.resItems[i].r_type)
                    break;
                default :
                    item = new createjs.Bitmap(pic);
                    break;
            }
            // console.log(this.resItems[i].id);
            item.name = this.resItems[i].id;
            this.addChild(item);
            item.resIndex = i;
            // console.log(item,i)
            if(item.isDraw != undefined && item.isDraw != true){
                console.log(item,"has not be drawed")
                //设置触发动画属性对象
                return;
            }else{
                this.setScale(item,i);
                this.setInit(item,i);
                this.setPos(item,i);
                //animation
                this.setAnimation(item,i);
                this.setAction(item,i);
                //设置触发动画属性对象
                // console.log(this.resItems[i].Pages.page1.tweenTrigger);
                if(typeof this.resItems[i]["Pages"][this.name].tweenTrigger !=undefined){
                    item.tweenTrigger = this.resItems[i]["Pages"][this.name].tweenTrigger;
                }
                
            }



            //action

        }
        this.isDraw = true;
    }
    
    // add MyApp to myNameSpace
    window.eosi.website.BasePage_globalload = createjs.promote(BasePage_globalload, "ButtonBase");
//}());
}(window));





